

[SMGP13ACTIVETEST_RECV, SMGP13ACTIVETESTRESP_RECV, SMGP13LOGIN_RECV, SMGP13LOGINRESP_RECV, 
SMGP13SUBMIT_RECV, SMGP13SUBMITRESP_RECV, SMGP13DELIVER_RECV, SMGP13DELIVERRESP_RECV,
SMGP13EXIT_RECV, SMGP13EXITRESP_RECV, SMGP13QUERY_RECV, SMGP13QUERYRESP_RECV,
SMGP13FORWORD_RECV, SMGP13FORWORDRESP_RECV, SMGP13MTROUTEUPDATE_RECV, SMGP13MTROUTEUPDATERESP_RECV,
SMGP13MOROUTEUPDATE_RECV, SMGP13MOROUTEUPDATERESP_RECV] =  range(18)

[SMGP13ACTIVETEST_SEND, SMGP13ACTIVETESTRESP_SEND, SMGP13LOGIN_SEND, SMGP13LOGINRESP_SEND, 
SMGP13SUBMIT_SEND, SMGP13SUBMITRESP_SEND, SMGP13DELIVER_SEND, SMGP13DELIVERRESP_SEND,
SMGP13EXIT_SEND, SMGP13EXITRESP_SEND, SMGP13QUERY_SEND, SMGP13QUERYRESP_SEND,
SMGP13FORWORD_SEND, SMGP13FORWORDRESP_SEND, SMGP13MTROUTEUPDATE_SEND, SMGP13MTROUTEUPDATERESP_SEND,
SMGP13MOROUTEUPDATE_SEND, SMGP13MOROUTEUPDATERESP_SEND] =  range(18, 18 + 18)

SMGP13RECEIVERING, SMGP13RECEIVERED, SMGP13SENDING, SMGP13SENDED = range(18 + 18, 40)

SMGP13CONNECTING, SMGP13CONNECTED, SMGP13CONNECTFAIL, SMGP13CONNECTLOST = range(40, 44)

SMGP13PROTOCOLBUILD = 45


class Event(object):
    def __init__(self, type, data=None):
        self.type = type
        self.data = data
        
class EventManager(object):
    def __init__(self):
        self.dict = {}
    
    def _getEventType(self, eventType):
        if not self.dict.has_key(eventType):
            return None
            
        return self.dict[eventType]
    
    def _getEventSource(self, eventType, eventSource):
        d = self._getEventType(eventType)
        if d == None:
            return None
        if not d.has_key(eventSource):
            return None
        return d[eventSource]
    
    def _addEventType(self, eventType):
        if not self.dict.has_key(eventType):
            self.dict[eventType] = {}
            
        return self.dict[eventType]
    
    def _addEventSource(self, eventType, eventSource):
        d = self._addEventType(eventType)
        if not d.has_key(eventSource):
            d[eventSource] = []
        return d[eventSource]
    
    def Register(self, listener, eventType, eventSource='all'):
        l = self._addEventSource(eventType, eventSource)
        try:
            l.index(listener)
        except:
            l.append(listener)
       
    def _removeEventType(self, eventType):
        if self.dict.has_key(eventType):
            try:
                self.dict.pop(eventType)
            except:
                pass
            
    
    def _removeEventSource(self, eventType, eventSource):
        d = self._getEventType(eventType)
        if not d.has_key(eventSource):
            d[eventSource] = []
        return d[eventSource]
    
    def UnRegister(self, listener, eventType = None, eventSource = None):
        if eventType == None:
            if eventSource == None:
                for k,v in self.dict:
                    for k1, v1 in v:
                        try:
                            v1.remove(listener)
                        except:
                            pass
            else:
                for k, v in self.dict:
                    l = v.get(eventSource, None)
                    if l == None:
                        continue
                    else:
                        try:
                            v1.remove(listener)
                        except:
                            pass
        else:
            if eventSource == None:
                v = self.dict.get(eventType, None)
                if v == None:
                    return
                else:
                    for k1, v1 in v:
                        try:
                            v1.remove(listener)
                        except:
                            pass
            else:
                v = self.dict.get(eventType, None)
                if v == None:
                    return
                else:
                    v1 = v.get(eventSource, None)
                    if v1 == None:
                        return
                    else:
                        try:
                            v1.remove(listener)
                        except:
                            pass            
    
    def fire(self, event, eventSource):
#        if eventSource == None:
#            d = self._getEventType(event.type)
#            if d == None:
#                return
#            for k, v in d:
#                for f in v:
#                    f(event)                    
#        else:
        d = self._getEventType(event.type)
        if d != None:            
            v = d.get('all', [])
            for f in v:
                f(event)
            
        l = self._getEventSource(event.type, eventSource)
        if l == None:
            return
        for f in l:
            f(event)
            
eventMgr = EventManager()

class Test(object):
    def __init__(self):
        self.data = 'test'
    
    def create(self):        
        eventMgr.fire(Event(SMGP13CONNECTLOST, 'TEST creact...'), self)
        
    def msg(self, msg):
        eventMgr.fire(Event(SMGP13CONNECTED, msg), self)
        
    def p(self, msg):
        print self.data + msg.data
        
def pp(x):
    print str(x.data)
def pp1(x):
    print 'pp1' + str(x.data)

def c(f, msg):
    f(msg)    

if __name__ == '__main__':
    t = Test()
    t2 = Test()
    eventMgr.Register(pp, SMGP13CONNECTLOST, t)
    eventMgr.Register(pp1, SMGP13CONNECTED, t)
    eventMgr.Register(lambda evt:c(pp, evt), SMGP13CONNECTED)

    t.msg('tst1')
    





